<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :formType="type"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import {
  addColor,
  updateColor,
  addColorOEM,
  updateColorOEM
 } from '@/api/common/cars/colorCars';
import { dictionary } from '@/utils/dictMixin';
import Cookies from 'js-cookie';

export default {
  name: 'ColorModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      dataType: Cookies.get('dataType'),// 判断是否为厂端
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.colorId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      let dataType = Cookies.get('dataType');// 判断是否为厂端
      return [
        {
          type: 'SELECT',
          label: '颜色类型',
          fieldName: 'colorType',
          placeholder: '请选择',
          disabled: this.type === 'edit'?true:false,
          itemList: this.createDictList('1608'),
          rules: [{ required: true, message: '请选择颜色类型', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '颜色代码',
          disabled: this.type === 'edit'?true:false,
          fieldName: 'colorCode',
          placeholder: '颜色代码',
          rules: [{ required: true, message: '请输入颜色代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '颜色名称',
          fieldName: 'colorName',
          placeholder: '颜色名称',
          rules: [{ required: true, message: '请输入颜色名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '数据来源',
          hidden: dataType === '10461001'?false:true,
          disabled: true,
          fieldName: 'dataSources',
          placeholder: '请选择',
          initialValue: 10451001,
          itemList: this.createDictList('1045'),
          rules: [{ required: true, message: '请选择数据来源', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择是否有效', trigger: 'blur' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.saveData.recordVersion = this.rowData.recordVersion
        let dataType = Cookies.get('dataType');// 判断是否为厂端
        let res = {};
        if (this.type === 'add') {
          if (dataType === '10461001') {
            res = await addColor(this.saveData);
          }else{
            res = await addColorOEM(this.saveData);
          }
        }
        if (this.type === 'edit') {
          if (dataType === '10461001') {
            res = await updateColor(this.saveData, this.recordId);
          }else{
            res = await updateColorOEM(this.saveData, this.recordId);
          }
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
